import {
  getIndicatorSystem,
  getIndicatorSystemPermissions,
  getByCategoryId,
  getOneLevelIndicatorsPermissions,
  getByLevel,
  getByLevelPermissions,
  listValidByTwoId
} from '@/api/common/target'
import { getListRegionInfo, getListRegionInfoPermissions } from '@/api/common/region'

import { useUserStore } from '@/store/modules/user'
const userStore = useUserStore()

/**
 * 指标体系入参
 */
export interface Params {
  hasDefault?: boolean
  hasPermissions?: boolean
  raw?: boolean
}
/**
 * 返回结果
 */
interface Result {
  label: string
  value: string | number
  raw?: object
}

export default () => {
  /**
   * 指标体系
   * @param params 入参
   * @returns
   */
  async function useIndicatorSystem(params?: Params): Promise<Result[]> {
    const indicatorSystemParams = {
      pageIndex: 1,
      pageNum: 1000,
      asc: false,
      order: 'create_dt',
      state: 'N'
    }
    try {
      const res = params?.hasPermissions
        ? await getIndicatorSystemPermissions(indicatorSystemParams)
        : await getIndicatorSystem(indicatorSystemParams)
      return res.data.records.map((item) => {
        const data: Result = {
          label: item.name,
          value: item.id
        }
        if (params?.raw) {
          data.raw = item
        }
        return data
      })
    } catch {
      return []
    }
  }

  /**
   * 一级指标
   * @param params 入参
   * @returns
   */
  async function useOneLevel(categoryId: string | number, params?: Params): Promise<Result[]> {
    const oneLevelParams = {
      pageIndex: 1,
      pageNum: 1000,
      categoryId,
      state: 'N',
      regionCd: userStore?.deptInfo?.regionCode,
      asc: true,
      order: 'code'
    }
    try {
      if (params?.hasPermissions) {
        const res = await getOneLevelIndicatorsPermissions(oneLevelParams)
        return res.data.records.map((item) => {
          const data: Result = {
            label: item.name,
            value: item.id
          }
          if (params?.raw) {
            data.raw = item
          }
          return data
        })
      } else {
        const res = await getByCategoryId({ categoryId })
        return res.data.map((item) => {
          const data: Result = {
            label: item.name,
            value: item.id
          }
          if (params?.raw) {
            data.raw = item
          }
          return data
        })
      }
    } catch {
      return []
    }
  }

  /**
   * 二级指标
   * @param params 入参
   * @returns
   */
  async function useTwoLevel(levelOneId: string | number, params?: Params): Promise<Result[]> {
    try {
      const res = params?.hasPermissions
        ? await getByLevelPermissions({ levelOneId })
        : await getByLevel({ levelOneId })
      return res.data.map((item) => {
        const data: Result = {
          label: item.name,
          value: item.id
        }
        if (params?.raw) {
          data.raw = item
        }
        return data
      })
    } catch {
      return []
    }
  }

  /**
   * 监测事项
   * @param params 入参
   * @returns
   */
  async function useMonitoringItems(
    levelTwoId: string | number,
    params?: Params
  ): Promise<Result[]> {
    try {
      const res = await listValidByTwoId({ levelTwoId })
      return res.data.map((item) => {
        const data: Result = {
          label: item.content,
          value: item.id
        }
        if (params?.raw) {
          data.raw = item
        }
        return data
      })
    } catch {
      return []
    }
  }

  /**
   * 获取区域
   * @param params 入参
   * @returns
   */
  async function useRegion(standard: string | number = 3, params?: Params): Promise<Result[]> {
    try {
      const res = params?.hasPermissions
        ? await getListRegionInfoPermissions({ standard })
        : await getListRegionInfo({ standard })
      return res.data.map((item) => {
        const data: Result = {
          label: item.regionName,
          value: item.regionCode
        }
        if (params?.raw) {
          data.raw = item
        }
        return data
      })
    } catch {
      return []
    }
  }

  return {
    useIndicatorSystem,
    useOneLevel,
    useTwoLevel,
    useMonitoringItems,
    useRegion
  }
}
